package cn.liberals.pet.service.impl;

import cn.liberals.basic.utils.BaiduAuditUtils;
import cn.liberals.pet.domain.Pet;
import cn.liberals.pet.domain.PetDetail;
import cn.liberals.pet.domain.PetOnlineAuditLog;
import cn.liberals.pet.mapper.PetDetailMapper;
import cn.liberals.pet.mapper.PetMapper;
import cn.liberals.pet.mapper.PetOnlineAuditLogMapper;
import cn.liberals.pet.service.IPetService;
import cn.liberals.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liberals
 * @since 2022-07-16
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;


    /**
     * @Title: save
     * @Description: 联动新增pet、petDetail表
     * @Author: Mr.Tarry
     * @Version: 1.0
     * @Date:  2022/7/16 22:51
     * @Parameters: [pet]
     * @Return void
     */
    @Override
    public void save(Pet pet) {
        petMapper.save(pet);
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.save(detail);
    }

    /**
     * @Title: remove
     * @Description: 联动删除pet、petDetail表
     * @Author: liberals
     * @Version: 1.0
     * @Date:  2022/7/16 22:56
     * @Parameters: [id]
     * @Return void
     */
    @Override
    public void remove(Long id) {
        Pet pet = petMapper.loadById(id);
        PetDetail petDetail =  petDetailMapper.loadByPetId(pet.getId());
        if (petDetail != null) {
            petDetailMapper.remove(petDetail.getId());
        }
        petMapper.remove(id);
    }

    /**
     * @Title: update
     * @Description: 联动修改pet、petDetail表
     * @Author: liberals
     * @Version: 1.0
     * @Date:  2022/7/16 22:59
     * @Parameters: [pet]
     * @Return void
     */
    @Override
    public void update(Pet pet) {
        petMapper.update(pet);
        PetDetail detail = pet.getDetail();
        if (detail.getIntro()!=null ||detail.getAdoptNotice()!=null){
            petDetailMapper.update(detail);
        }
    }

    @Override
    /* 
    * @Title: onsale
    * @Description: 上架
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/17 2:23  
    * @Parameters: 
    * @Return 
    */
    public void onsale(List<Long> ids) {
        // 遍历Ids
        for (Long id : ids) {
            // 1.得到每个宠物基本信息对象
            Pet pet = petMapper.loadById(id);
            StringBuffer sbf = new StringBuffer();
            // 2.调用百度AI进行文字，图片的审核
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                sbf.append("宠物名称不合法，宠物名称为：" + pet.getName());
            }
            /*if (!BaiduAuditUtils.ImgCensor("123.207.27.208" + pet.getResources().split(",")[0])){
                sbf.append("宠物图片不合法，宠物图片路径：" + pet.getResources().split(",")[0]);
            }*/
            if (sbf.length() > 0){
                // 2.1.如果违规，我们要保存审核失败日志
                PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                petOnlineAuditLog.setPetId(id);
                petOnlineAuditLog.setNote(sbf.toString());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                continue;
            }
            petMapper.onsale(id);
        }
    }

    /*
    * @Title: offsale
    * @Description: 下架
    * @Author: liberals
    * @Version: 1.0
    * @Date:  2022/7/17 2:24
    * @Parameters:
    * @Return
    */
    @Override
    public void offsale(List<Long> ids) {
        petMapper.offsale(ids);
    }
}
